package cn.z2huo.leetcode.problem.NO_0004;

import java.util.Arrays;

/**
 * <p>
 *
 * @author z2huo
 */
class FindMedianSortedArraysSolution {

    /**
     * <p>最简单的方式，将两个数组合并，之后对合并后的数组排序，然后计算中位数的值
     *
     * @param nums1 数组
     * @param nums2 数组
     * @return  中位数
     */
    public double mergeArrayAndSort(int[] nums1, int[] nums2) {

        int[] mergeArray = new int[nums1.length + nums2.length];

        System.arraycopy(nums1, 0, mergeArray, 0, nums1.length);
        System.arraycopy(nums2, 0, mergeArray, nums1.length, nums2.length);

        Arrays.sort(mergeArray);

        return (double) (mergeArray[mergeArray.length / 2] + mergeArray[(mergeArray.length - 1) / 2]) / 2;
    }

    /**
     * <p>将两个有序数组归并成一个有序数组，之后求该有序数组的中位数
     *
     * @param nums1 有序数组
     * @param nums2 有序数组
     * @return  中位数
     */
    public double mergeSortedArray(int[] nums1, int[] nums2) {

        int[] mergeArray = new int[nums1.length + nums2.length];

        int mergeIdx = 0;
        int idx = 0, idx2 = 0;
        while (idx < nums1.length && idx2 < nums2.length) {
            if (nums1[idx] < nums2[idx2]) {
                mergeArray[mergeIdx++] = nums1[idx++];
            } else if (nums1[idx] > nums2[idx2]) {
                mergeArray[mergeIdx++] = nums2[idx2++];
            } else {
                mergeArray[mergeIdx++] = nums1[idx++];
                mergeArray[mergeIdx++] = nums2[idx2++];
            }
        }
        while (idx < nums1.length) {
            mergeArray[mergeIdx++] = nums1[idx++];
        }
        while (idx2 < nums2.length) {
            mergeArray[mergeIdx++] = nums2[idx2++];
        }

        return (double) (mergeArray[mergeArray.length / 2] + mergeArray[(mergeArray.length - 1) / 2]) / 2;
    }

    public double findMedianSortedArrays(int[] nums1, int[] nums2) {



        return 0;
    }

}
